package net.thxjj.adminServer.service.impl;

import net.thxjj.adminServer.dao.DeviceDao;
import net.thxjj.adminServer.entity.LoginHadleEvent;
import net.thxjj.adminServer.entity.onenet.OnetEvent;
import net.thxjj.adminServer.mongodao.MongoLoginHandleDao;
import net.thxjj.adminServer.mongodao.MongoOnenetDao;
import net.thxjj.adminServer.service.IDevOneNetLogService;
import net.thxjj.adminServer.util.ParameterMap;
import net.thxjj.sitermodel.Device;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DevOneNetLogService implements IDevOneNetLogService {

    @Autowired
    private DeviceDao deviceDao;

    @Autowired
    private MongoOnenetDao mongoOnenetDao;

    @Autowired
    private MongoLoginHandleDao mongoLoginHandleDao;

    @Async("asyncExecutor")
    @Override
    public void addEvent(List<OnetEvent> list) {
        mongoOnenetDao.saveOnetDatas(list);
    }

    @Async("asyncExecutor")
    @Override
    public void mqAddDevLog(OnetEvent devLog) {
        mongoOnenetDao.saveOnetData(devLog);
    }

    @Override
    public OnetEvent getSmokeStatus(String deviceId) {
       return mongoOnenetDao.getSmokeStatus(deviceId);
    }

    @Override
    public OnetEvent getSmokeLoginEvent(String deviceId) {
        return mongoOnenetDao.getSmokeLoginEvent(deviceId);
    }

    @Override
    public ParameterMap isMultiLoginError(String deviceId) {
        List<LoginHadleEvent> loginHandleInfo = mongoLoginHandleDao.getAllLoginHandleInfo(deviceId);
        long timestamp = 0;
        long value = 0;
        if(loginHandleInfo==null || loginHandleInfo.size()==0){
            OnetEvent onetEvent = mongoOnenetDao.getSmokeLoginEvent(deviceId);
            if(onetEvent!=null){
                value = 1;
                timestamp = onetEvent.getCreateTime();
            }
        }else{
            long a = loginHandleInfo.get(loginHandleInfo.size()-1).getTimestamp();
            List<OnetEvent> list = mongoOnenetDao.getSmokeLoginEventList(deviceId,a);
            if(list.size()>0){
                value = 1;
                timestamp = list.get(list.size()-1).getCreateTime();
            }
        }

        ParameterMap map = new ParameterMap();
        map.put("datasetId","multlogin");
        map.put("value",value);
        map.put("timestamp",timestamp);
        return map;
    }

    @Override
    public List<List<Object>> getLoginErrList(String device_id) {
        List<LoginHadleEvent> list =  mongoOnenetDao.getLoginHandleWithLog(device_id);
        List<List<Object>> listLogin = new ArrayList<>();
        for (LoginHadleEvent map:list) {
            long ad = map.getUserId();
            long value = 1;
            if(ad>0){
                value = 2;
            }
            long timestamp = map.getTimestamp();
            List<Object> lista = new ArrayList<>();
            lista.add(timestamp);
            lista.add(value);
            listLogin.add(lista);
        }
        return listLogin;
    }

    @Override
    public ParameterMap HeartBeat(String device_id) {
        List<OnetEvent> onetEventList = mongoOnenetDao.getSmokeStatusList(device_id);
       long heartbeart = onetEventList.size();
        if(heartbeart<=0){
            ParameterMap map = new ParameterMap();
            map.put("datasetId","heartbeat");
            map.put("value",0);
            return map;
        }

        long set_heart_time;
        Device device = deviceDao.getDeviceInfo(device_id);
        if(device.getHeartTime()<=0){
            set_heart_time = 1200;
        }else {
            set_heart_time = device.getHeartTime();
        }
        long heartbeart_lilun = 0;
        long heartlv = 0;
        OnetEvent data = onetEventList.get(0);
        if(data!=null){
            long timelong = data.getCreateTime();
            long cur_timelong = System.currentTimeMillis();
            if(cur_timelong>timelong){
                long a  = cur_timelong - timelong;
                heartbeart_lilun = (a/(set_heart_time*60000))+1;
            }
        }
        if(heartbeart>heartbeart_lilun){
            heartlv =100;
        }else{
            heartlv = heartbeart * 100 / heartbeart_lilun;
        }
        ParameterMap map = new ParameterMap();
        map.put("datasetId","heartbeat");
        map.put("value",heartlv);
        return map;
    }


    @Override
    public List<OnetEvent> getSmokeStatusList(String device_id, long start_time, long end_time) {
        return mongoOnenetDao.getSmokeStatusListByTime(device_id,start_time,end_time);
    }

    @Override
    public List<OnetEvent> getSmokeAllDataList(String device_id, long start_time, long end_time) {
        return mongoOnenetDao.getSmokeAllListByTime(device_id,start_time,end_time);
    }

    @Override
    public OnetEvent getSmokeNewestStatus(String device_id, long time) {
        return mongoOnenetDao.getNewestSmokeStatus(device_id,time);
    }

    @Override
    public void updateOnlineStatus(boolean flag, String deviceId,long timestamp) {
        if(flag){
           ParameterMap poa = new ParameterMap();
           poa.put("device_id",deviceId);
           poa.put("online_at",timestamp);
           deviceDao.updateDeviceOnLine(poa);
        }else{
            ParameterMap poa = new ParameterMap();
            poa.put("device_id",deviceId);
            poa.put("offline_at",timestamp);
            deviceDao.updateDeviceOnLine(poa);
        }
    }
}
